﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace DeepTalkClientWebServer
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class HiddenWindow : Window
    {
        private NotifyIcon _systray;

        public HiddenWindow()
        {
            InitializeComponent();

            ///
            /// Get the directory
            /// 

            string[] args = System.Environment.GetCommandLineArgs();
            if (args.Length < 2)
            {
                Close();
            }
            DirectoryInfo info = new DirectoryInfo (args[1]);
            if (!info.Exists)
            {
                Close();
            }

            ///
            /// Create the sys-tray icon
            /// 

            _systray = new NotifyIcon();
            _systray.Icon = new System.Drawing.Icon(typeof(Control), "Error.ico");
            _systray.Text = "Viewer for " + info.Name;

            ///
            /// The menu
            /// 

            ContextMenu cm = new ContextMenu();

            MenuItem itemExit = new MenuItem("E&xit");
            itemExit.Click += new EventHandler(menu_exit);

            cm.MenuItems.Add(itemExit);

            _systray.ContextMenu = cm;
            _systray.Visible = true;

            Random r = new Random();
            int port = 0;
            bool done = false;
            while (!done)
            {
                port = r.Next(1024, 65535);
                done = StartServer(info.FullName, port);
            }

            ///
            /// Next, open up a web browser to watch over this url
            /// 

            StartWebBrowser("http://localhost:" + port.ToString());
            
        }

        /// <summary>
        /// Starts the default web browser using the specified url
        /// </summary>
        /// <param name="p"></param>
        private void StartWebBrowser(string url)
        {
            Process browser = new Process();
            browser.EnableRaisingEvents = true;
            browser.StartInfo = FindProperWebBrowser.DefaultBrowserInfo();
            browser.StartInfo.Arguments = browser.StartInfo.Arguments + "  \"" + url + "\"";

            browser.Exited += new EventHandler(browser_Exited);

            browser.Start();
        }

        /// <summary>
        /// User exited the browser. Now time to close up shop!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void browser_Exited(object sender, EventArgs e)
        {
            Dispatcher.Invoke((Action)delegate { Close(); });
        }

        /// <summary>
        /// User wants to exit now!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void menu_exit(object sender, EventArgs e)
        {
            Dispatcher.Invoke((Action)delegate { Close(); });
        }

        private TinyServerLib.TinyServer _server;

        /// <summary>
        /// Start up a small local web server on a particular port.
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private bool StartServer(string baseAddr, int port)
        {
            _server = new TinyServerLib.TinyServer();

            _server.DefaultPage = "ViewConference.html";
            _server.LogLevel = TinyServerLib.LogKind.None;
            _server.Port = port;
            _server.WebRootPath = baseAddr;
            _server.Templates = "c:\\temp"; // Shouldn't be used

            _server.Run();
            System.Threading.Thread.Sleep(100);

            return _server.Running;
        }
    }
}
